

import {
    promptBox,
    toast,
    base
} from 'nc-lightapp-front';

import deepCopy from '../../public/functions/deep-copy';

const {
    NCSelect
} = base;

const {NCOption} = NCSelect;

export default class TransferAction {
    constructor(comp) {
        this.comp = comp
    }
    // 提交穿梭框表单
    submitDesignModal = () => {
        const {
            dispatch,
            gloRep
        } = this.comp.props;


        dispatch({
            type: 'gloRep/submitDesignModal'
        })
            .then((res) => {
                if(res.success) {
                    toast({
                        colors: 'success',
                        content: gloRep.language['hi6007-000208'] // 设计成功
                    });

                    this.closeDesignModal();
                }
            });
    }

    // 如果选择了排序是升序或者降序
    selectSortType = (c) => {
        return (value) => {
            const {dispatch, gloRep} = this.comp.props;
            const {transferSortList} = gloRep;

            const nList = [...transferSortList];

            nList.forEach((item) => {
                if(item.key === c.key) {
                    item.is_asc = value;
                }
            });

            dispatch({
                type: 'gloRep/update',
                payload: {
                    transferSortList: this.processTransferSortList(nList)
                }
            });
        }
    }


    // 处理排序的穿梭框的数据
    processTransferSortList = (data) => {

        data.map((item) => {
            item['suffix'] = (
                <div className="transfer-sort-type">
                    <NCSelect
                        value={item.is_asc || 'Y'}
                        onSelect={this.selectSortType(item)}
                    >
                        <NCOption
                            value="Y"
                        >
                            升序
                        </NCOption>
                        <NCOption
                            value="N"
                        >
                            降序
                        </NCOption>
                    </NCSelect>
                </div>
            );
        });
        
        return data;

    }

    // 穿梭框步骤
    turnStep = (toStep) => {

        return () => {
            const {
                dispatch,
                gloRep
            } = this.comp.props;

            if(toStep === 1) {
                // console.log(gloRep.transferList2)
                dispatch({
                    type: 'gloRep/update',
                    payload: {
                        transferCurrentStep: toStep,
                        transferTargetData: this.processTransferData(gloRep.transferList2, [], 1),
                        transferCheckedList: {},
                        ifHighLight: {
                            up: false,
                            upp: false,
                            down: false,
                            downn: false
                        }
                    }
                });
            }
            else {
                // console.log(gloRep.transferTargetData)
                // console.log(gloRep.transferSortList)
               
                let transferTargetData = this.processTransferData(gloRep.transferTargetData, gloRep.transferSortList, toStep)
                // let processedTransferData = this.processTransferData(gloRep.transferTargetData, gloRep.transferSortList, toStep)
                // let transferTargetData = addNewTargetData.concat(processedTransferData)
                // console.log('transferTargetData', transferTargetData)
                dispatch({
                    type: 'gloRep/update',
                    payload: {
                        transferCurrentStep: toStep,
                        // transferList2: deepCopy(gloRep.transferTargetData),
                        transferTargetData: transferTargetData,
                        transferCheckedList: {},
                        ifHighLight: {
                            up: false,
                            upp: false,
                            down: false,
                            downn: false
                        }
                    }
                });
            }
            
        }

    }
    // 右侧穿梭框上下移动
    moveTransferItem = (type) => {
        return () => {
            const {
                dispatch,
                gloRep
            } = this.comp.props;
            const {
                transferCheckedList,
                transferCurrentStep,
                ifHighLight
            } = gloRep;

            if(!ifHighLight[type]) {
                return;
            }

            let nIfHighLight = {
                ...ifHighLight
            };

            let moveList = [];  
            let restList = [];

            let result = [];

            let processDataName = 'transferSortList';

            if(transferCurrentStep === 1) {
                processDataName = 'transferTargetData';
            }

            nIfHighLight = {
                upp: true,
                up: true,
                down: true,
                downn: true
            };

            if(type === 'upp' || type === 'downn') {
                gloRep[processDataName].map((item, index) => {
                    if(transferCheckedList[item.key] && transferCheckedList[item.key].status) {
                        moveList.push(item);
                    }
                    else {
                        restList.push(item);
                    }
                });
                if(moveList.length === 1) {
                    if(type === 'upp') {
                        nIfHighLight = {
                            upp: false,
                            up: false,
                            down: true,
                            downn: true
                        };
                    }
                    else {
                        nIfHighLight = {
                            upp: true,
                            up: true,
                            down: false,
                            downn: false
                        };
                    }
                }
                if(type === 'upp') {
                    result = moveList.concat(restList);
                }
                else {
                    result = restList.concat(moveList);
                }
            }
            else if(type === 'up') {
                let nTargetData = [...gloRep[processDataName]];
                gloRep[processDataName].map((item, index) => {
                    if(transferCheckedList[item.key] && transferCheckedList[item.key].status) {
                        if(index > 0) {
                            let tmp = nTargetData[index];
                            nTargetData[index] = nTargetData[index - 1];
                            nTargetData[index - 1] = tmp;

                            if(index === 1) {
                                nIfHighLight = {
                                    upp: false,
                                    up: false,
                                    down: true,
                                    downn: true
                                };
                            }
                        }
                    }
                });
                result = nTargetData;
            }
            else if(type === 'down') {
                let nTargetData = [...gloRep[processDataName]].reverse();
                gloRep[processDataName].reverse().map((item, index) => {
                    if(transferCheckedList[item.key] && transferCheckedList[item.key].status) {
                        if(index > 0) {
                            let tmp = nTargetData[index];
                            nTargetData[index] = nTargetData[index - 1];
                            nTargetData[index - 1] = tmp;

                            if(index === 1) {
                                nIfHighLight = {
                                    upp: true,
                                    up: true,
                                    down: false,
                                    downn: false
                                };
                            }
                        }
                    }
                });
                result = [...nTargetData].reverse();
            }

            dispatch({
                type: 'gloRep/update',
                payload: {
                    [processDataName]: result,
                    ifHighLight: nIfHighLight
                }
            });
        }
    }
    // 穿梭框复选框选中和取消选中的时候
    onCheckedTransfer = (type, checkedList) => {
        const {
            dispatch,
            gloRep: {
                transferCurrentStep,
                transferTargetData,
                transferSortList,
                ifHighLight
            }
        } = this.comp.props;

        let nIfHighLight = {
            ...ifHighLight
        };

        let onlyFirst = true;
        let onlyLast = true;
        let hasChecked = false;

        let opData = {
            1: transferTargetData,
            2: transferSortList
        }[transferCurrentStep];

        Object.keys(checkedList).forEach((key, index) => {
            if(type === 'right' && checkedList[key].status) {  
                if(opData[0].key !== key) {
                    onlyFirst = false;
                }
                if(opData[opData.length - 1].key !== key) {
                    onlyLast = false;
                }
                hasChecked = true;
            }
        });

        if(type === 'right') {
            nIfHighLight = {
                upp: !onlyFirst,
                up: onlyFirst ? false : hasChecked,
                down: onlyLast ? false : hasChecked,
                downn: !onlyLast
            };
        }
        
        dispatch({
            type: 'gloRep/update',
            payload: {
                transferCheckedList: checkedList,
                ifHighLight: nIfHighLight
            }
        });
    }

    // 穿梭框修改组织信息
    onChangeInfoGroup = (value, e) => {

        const {
            dispatch,
            gloRep
        } = this.comp.props;

        const {
            transferDataSource,
            transferTargetData,
            subSetMap
        } = gloRep;


        dispatch({
            type: 'gloRep/changeInfoGroup',
            payload: {
                pk_infoset: value
            }
        })
            .then((res) => {
                dispatch({
                    type: 'gloRep/update',
                    payload: {
                        transferDataSource: this.processTransferData(res.data, transferTargetData),
                        transferCurrentInfoGroup: value,
                        subSetMap: {
                            ...subSetMap,
                            [value]: deepCopy(this.processTransferData(res.data))
                        }
                    }
                });
            });
    }

    // 转换穿梭框数据
    processTransferData = (list, ignoreList = [], toStep) => { // list是上一步里选中到右边的所有数据
        const {
            dispatch,
            gloRep
        } = this.comp.props;
        let nList = {};
        let ignoreKey = {};
        

if(toStep === 2) {
     // 组织编码、部门编码、人员显示顺序、组织显示顺序、部门显示顺序
     let addNewTargetData = [
        { 
        title: gloRep.language["hi6007-000304"] || "组织编码",
        table_name: "org_orgs",
        key: "1001Z710000000001SEL",
        display: gloRep.language["hi6007-000304"] || "组织编码",
        suffix: null,
        prefix: null,
        },{
            title: gloRep.language["hi6007-000305"] || "部门编码",
            table_name: "org_dept",
            key: "1001Z710000000001S2M",
            display: gloRep.language["hi6007-000305"] || "部门编码",
            suffix: null,
            prefix: null,   
        },{
            title: gloRep.language["hi6007-000306"] || "人员显示顺序",
            table_name: "hi_psnjob",
            key: "1001Z710000000000WOE",
            display: gloRep.language["hi6007-000306"] || "人员显示顺序",
            suffix: null,
            prefix: null, 
        },
        // {
        //     title: "组织显示顺序",
        //     table_name: "",
        //     key: "4",
        //     display: "组织显示顺序",
        //     suffix: null,
        //     prefix: null, 
        // },
        {
            title: gloRep.language["hi6007-000307"] || "部门显示顺序",
            table_name: "org_dept",
            key: "1001Z710000000001S33",
            display: gloRep.language["hi6007-000307"] || "部门显示顺序",
            suffix: null,
            prefix: null, 
        }
    ]
    for (let i = addNewTargetData.length-1; i >= 0; i--) {
        const addDataItem = addNewTargetData[i]
        for (let s = 0; s < list.length; s++) {
            if (addDataItem.key === list[s].key) {
                addNewTargetData.splice(i, 1);
            }
        }
    }

    list = addNewTargetData.concat(list)
    
    dispatch({
        type: 'gloRep/update',
        payload: {
            transferList2: deepCopy(list),
        }
    });
} else if(toStep === 1){
    // 第一步进入的时候过滤掉那四个字段
    let addDataKeys = ["1001Z710000000001SEL", "1001Z710000000001S2M", "1001Z710000000000WOE", "1001Z710000000001S33"]
    let filterList = list.filter((item, index) => {
        return !addDataKeys.includes(item.key)
    })
    list = filterList
}

        ignoreList.forEach((item) => {  // 已经 选到右边的数据
            ignoreKey[item.key] = true;
        });

        let result = [];

        list.forEach((item) => {
            if(!ignoreKey[item.key]) {
                if(nList[item.title]) {
                    item['display'] = item.title + ' - ' + item.table_name;
                    nList[item.title]['display'] = nList[item.title].title + ' - ' + nList[item.title].table_name;
                }
                else {
                    item['display'] = item.title;
                    nList[item.title] = item;
                }
                !item['suffix'] && (item['suffix'] = null);
                !item['prefix'] && (item['prefix'] = null);

                result.push(deepCopy(item));
            }
        });
        return result;
    }

    // 设计弹窗的穿梭框变化
    transferChange = (leftList, rightList) => {
        const {
            dispatch,
            gloRep
        } = this.comp.props;

        const {
            transferCurrentStep,
            subSetMap,
            transferCurrentInfoGroup
        } = gloRep;

        if(transferCurrentStep === 1) {
            // 此处是为了右侧穿梭回左侧的时候，判断是否是这个子集的左侧数据，如果不是的话不加进去
            let nLeftList = [];

            if(subSetMap[transferCurrentInfoGroup]) {
                let list = [];
                subSetMap[transferCurrentInfoGroup].map((item) => {
                    list.push(item.key);
                });

                leftList.map((item) => {
                    if(list.includes(item.key)) {
                        nLeftList.push(item);
                    }
                });
            }
            else {
                nLeftList = leftList;
            }

            dispatch({
                type: 'gloRep/update',
                payload: {
                    transferDataSource: nLeftList,
                    transferTargetData: this.processTransferData(rightList),
                    transferSortList: []
                }
            });
        }
        else {
            leftList.map((item) => {
                if(item.suffix) {
                    item.suffix = null;
                }
            });
            dispatch({
                type: 'gloRep/update',
                payload: {
                    transferTargetData: this.processTransferData(leftList),
                    transferSortList: this.processTransferSortList(rightList)
                }
            });
        }
        
    }

    // 设计花名册报表初始化数据
    designReport = () => {
        const {
            gloRep,
            dispatch
        } = this.comp.props;
        const {
            selectedRow,
            subSetMap
        } = gloRep;

        dispatch({
            type: 'gloRep/checkDesignModal'
        })
            .then((res) => {
                if(selectedRow && selectedRow.values['rpt_type'].value === '1') {
                    location.port ? window.open("uclient://start/http://" + location.hostname + ":" + location.port + res.data) : window.open("uclient://start/http://" + location.hostname + res.data)
                }
                else {
                    dispatch({
                        type: 'gloRep/getDesignInitData'
                    })
                        .then((res) => {
                                dispatch({
                                    type: 'gloRep/update',
                                    payload: {
                                        transferDataSource: this.processTransferData(res.data.leftList, res.data.rightList),
                                        transferTargetData: this.processTransferData(res.data.rightList, [], 1),
                                        transferSortList: this.processTransferData(this.processTransferSortList(res.data.sortList)),
                                        transferOpts: res.data.opts,
                                        transferCurrentInfoGroup: res.data.opts[0].value,
                                        subSetMap: {
                                            ...subSetMap,
                                            [res.data.opts[0].value]: deepCopy(this.processTransferData(res.data.leftList))
                                        }
                                    }
                                });
                                this.comp.actions.toggleModal('designReportModal', true)();
                        });
                }
            });

        
    }

    closeDesignModal = () => {
        const {
            dispatch
        } = this.comp.props;
        this.comp.actions.toggleModal('designReportModal', false)();
        dispatch({
            type: 'gloRep/update',
            payload: {
                transferDataSource: [],
                transferTargetData: [],
                transferSortList: [],
                transferOpts: [],
                transferCurrentInfoGroup: '',
                transferCheckedList: [],
                transferCurrentStep: 1,
                transferList2: []
            }
        });
    }

}